/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import MouseSensorDriver.GraphListener;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author Asko
 */
public class ScriptAnalyzer {

    StringTokenizer token;
    String source = "";
    EndoscopExp endExp;
    EndoscopeExpContainer container;
    EndoscopeChannel[] channels;
    ArrayList<GraphListener> scanList = new ArrayList<>();
    boolean airship = true;
    GraphListener sl;

    public ScriptAnalyzer(String source, EndoscopeExpContainer container,
            EndoscopeChannel[] channels) {
        this.channels = channels;
        this.source = source;
        this.container = container;
        token = new StringTokenizer(source, "; \t" + (char) 10 + (char) 13);
    }

    public void setSource(String source) {
        this.source = source;
        token = new StringTokenizer(source, "; \t" + (char) 10 + (char) 13);
    }

    public void startAnalyze() {
        while (token.countTokens() >= 4) {

            endExp = new EndoscopExp(channels[Integer.valueOf(token.nextToken())],
                    Double.valueOf(token.nextToken()),
                    Double.valueOf(token.nextToken()),
                    Double.valueOf(token.nextToken()));
            endExp.setTorqueLimit(Double.valueOf(token.nextToken()));
            endExp.addScanListener(sl);
            endExp.setAirship(airship);
            int type = Integer.valueOf(token.nextToken());
            switch (type) {
                case 0: {
                    endExp.startExp();
                    break;
                }
                case 1: {
                    endExp.scanExp();
                    break;
                }
                case 2: {
                    endExp.handManipulatingExp();
                    break;
                }
            }
            if (type != 2) {
                endExp.goHome();
            }
            endExp.removeScanListener(0);
            container.add(endExp);
        }
    }

    public void setAirship(boolean airship) {
        this.airship = airship;
    }

    public void addScanListener(GraphListener sl) {
        this.sl = sl;
    }

    public void removeScanListener(int indx) {
        scanList.remove(indx);
    }

    public void clearScanList() {
        scanList.clear();
    }

    public void translateToPlots(double eng, double adc) {
        for (int i = 0; i < scanList.size(); i++) {
            scanList.get(i).addValue(eng, adc);
        }
    }

    public void clearPlots() {
        for (int i = 0; i < scanList.size(); i++) {
            scanList.get(i).clear();
        }
    }
}
